package vip.liux.contracts.infrastructure.pay.dto.transfer;

import lombok.Data;
import lombok.experimental.Accessors;
import vip.liux.contracts.shared.enums.pay.PayTransferStatus;

import java.time.LocalDateTime;

/**
 * 统一转账 Response Dto
 *
 * @author jason
 */
@Data
@Accessors(chain = true)
public class ClientPayTransferDto {

    /**
     * 转账状态
     * <p>
     * 关联 {@link  PayTransferStatus}
     */
    private Integer status;

    /**
     * 外部转账单号
     */
    private String outTransferNo;

    /**
     * 支付渠道编号
     */
    private String channelTransferNo;

    /**
     * 支付成功时间
     */
    private LocalDateTime successTime;

    /**
     * 原始的返回结果
     */
    private Object rawData;

    /**
     * 调用渠道的错误码
     */
    private String channelErrorCode;
    /**
     * 调用渠道报错时，错误信息
     */
    private String channelErrorMsg;

    /**
     * 渠道 package 信息
     * <p>
     * 特殊：目前只有微信转账有这个东西！！！
     *
     * @see <a href="https://pay.weixin.qq.com/doc/v3/merchant/4012716430">JSAPI 调起用户确认收款</a>
     */
    private String channelPackageInfo;

    /**
     * 创建【WAITING】状态的转账返回
     */
    public static ClientPayTransferDto waitingOf(String channelTransferNo,
                                                 String outTransferNo, Object rawData) {
        ClientPayTransferDto respDto = new ClientPayTransferDto();
        respDto.status = PayTransferStatus.WAITING.getValue();
        respDto.channelTransferNo = channelTransferNo;
        respDto.outTransferNo = outTransferNo;
        respDto.rawData = rawData;
        return respDto;
    }

    /**
     * 创建【IN_PROGRESS】状态的转账返回
     */
    public static ClientPayTransferDto processingOf(String channelTransferNo,
                                                    String outTransferNo, Object rawData) {
        ClientPayTransferDto respDto = new ClientPayTransferDto();
        respDto.status = PayTransferStatus.PROCESSING.getValue();
        respDto.channelTransferNo = channelTransferNo;
        respDto.outTransferNo = outTransferNo;
        respDto.rawData = rawData;
        return respDto;
    }

    /**
     * 创建【CLOSED】状态的转账返回
     */
    public static ClientPayTransferDto closedOf(String channelErrorCode, String channelErrorMsg,
                                                String outTransferNo, Object rawData) {
        ClientPayTransferDto respDto = new ClientPayTransferDto();
        respDto.status = PayTransferStatus.CLOSED.getValue();
        respDto.channelErrorCode = channelErrorCode;
        respDto.channelErrorMsg = channelErrorMsg;
        // 相对通用的字段
        respDto.outTransferNo = outTransferNo;
        respDto.rawData = rawData;
        return respDto;
    }

    /**
     * 创建【SUCCESS】状态的转账返回
     */
    public static ClientPayTransferDto successOf(String channelTransferNo, LocalDateTime successTime,
                                                 String outTransferNo, Object rawData) {
        ClientPayTransferDto respDto = new ClientPayTransferDto();
        respDto.status = PayTransferStatus.SUCCESS.getValue();
        respDto.channelTransferNo = channelTransferNo;
        respDto.successTime = successTime;
        // 相对通用的字段
        respDto.outTransferNo = outTransferNo;
        respDto.rawData = rawData;
        return respDto;
    }

}
